#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include "debug.h"
#include "os_api.h"
#include "dev_api.h"
#include "sys_ver.h"

#define _STACK_SIZE		(1024*3)
static void* _task_stack = NULL;

static OSTaskRef _task_ref = NULL;

static OSATimerRef _task_delete_timer_ref = NULL;


void DeleteTaskByTimer(UINT32 id)
{
    OSA_STATUS status;

    if(_task_ref)
    {
        status = OSATaskDelete(_task_ref);
        ASSERT(status == OS_SUCCESS);
        _task_ref = NULL;
	   
        if(_task_stack)
        {
            free(_task_stack);
            _task_stack = NULL;
        }
    }

    OSATimerDelete (_task_delete_timer_ref);
}

static void _task(void *ptr)
{
	int count = 5;
	BOOL ret = FALSE;
	char imei_str[32] = {0};
	char sn_str[32] = {0};
	
    LOG_PRINTF("app_sdk_version: %s\n", APP_SDK_VERSION);
    LOG_PRINTF("dev_GetCpVersion: %s\n", dev_GetCpVersion());
    LOG_PRINTF("dev_GetSDKVersion: %s\n", dev_GetSDKVersion());
    

	while(count--){		

		memset(imei_str, 0x00, sizeof(imei_str));
		ret = dev_GetImei(imei_str, sizeof(imei_str));
		LOG_PRINTF("dev_GetImei ret %d, imei: %s\n", ret, imei_str);

		memset(sn_str, 0x00, sizeof(sn_str));
		ret = dev_GetSn(sn_str, sizeof(sn_str));
		LOG_PRINTF("dev_GetSn ret %d, sn: %s\n", ret, sn_str);
		
        OSATaskSleep(200);
	}

    if (count <= 0){
        //LOG_PRINTF("dev_CtrlReset.\n");
        //dev_CtrlReset(0);
        //LOG_PRINTF("dev_CtrlPoweroff.\n");
        dev_CtrlPoweroff(0);
    }
	
	OSATimerCreate(&_task_delete_timer_ref);
	OSATimerStart(_task_delete_timer_ref, 400, 0, DeleteTaskByTimer, 0);
}


int main(void)
{	
    int ret;

	_task_stack = malloc(_STACK_SIZE);
    ASSERT(_task_stack != NULL);
	
    ret = OSATaskCreate(&_task_ref, _task_stack, _STACK_SIZE, 100, "_task", _task, NULL);
    ASSERT(ret == OS_SUCCESS);
	
	return 0;
}